package com.huiztech.kaoqinji.service;

import android.content.Intent;
import android.os.IBinder;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.huiztech.kaoqinji.AppConst;
import com.huiztech.kaoqinji.mvc.KaoqinBaseService;

public class LocationService extends KaoqinBaseService {

    public LocationClient mLocClient;
    public MyLocationListenner myListener = new MyLocationListenner();

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mLocClient.stop();
    }

    @Override
    public void onCreate() {
        super.onCreate();
       // @SuppressWarnings("unused")
     //   LocApplication loc = (LocApplication) getApplication();
        mLocClient = new LocationClient(this);
        mLocClient.registerLocationListener(myListener);
        //sp = getSharedPreferences("user", MODE_PRIVATE);
        setLocationOption();
    }

    private void setLocationOption() {
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true);// 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setAddrType("all");
        option.setServiceName("com.baidu.location.service_v2.9");
        option.setScanSpan(Integer.valueOf(controller.sp.getString("interval", AppConst.MOREN_INTERVAL)) * 60000); // 时间间隔
        mLocClient.setLocOption(option);
        mLocClient.start();
    }

    /**
     * 定位监听函数
     */
    public class MyLocationListenner implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            if (location == null) return;
            if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS定位
                controller.doVisitServer(location.getLatitude(), location.getLongitude());
            } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 基站定位
                controller.doVisitServer(location.getLatitude(), location.getLongitude());
            }
        }

        public void onReceivePoi(BDLocation poiLocation) {
            if (poiLocation == null) {
                return;
            }
        }

    }
}
